
    /**  
    * @Title: CaiPiao.java
    * @Package com.third.caipiao
    * @Description: 
    * @author likai
    * @date 2017年9月15日
    * @version V1.0  
    */
    
package com.third.utils.jisuapi;

import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.supers.enums.Charset;
import com.utils.http.HttpClientUtil;
import com.utils.properties.PropertyUtil;
/**
    * @ClassName: CaiPiao
    * @Description: 极速数据彩票工具    官网：https://www.jisuapi.com/my/api.php
    * @author likai
    * @date 2017年9月15日
    *
    */

public class JisuUtil{
	public static Logger log = LoggerFactory.getLogger(JisuUtil.class);
	public  final String APPKEY;// 你的appkey
    public  final String type = "http://api.jisuapi.com/caipiao/class";
    public  final String query = "http://api.jisuapi.com/caipiao/query";
    public  final String history = "http://api.jisuapi.com/caipiao/history";
    public  final String winning = "http://api.jisuapi.com/caipiao/winning";
    
    public  final String msg = "http://api.jisuapi.com/sms/send";//短信接口
    
    
    private Properties prop;
    private static JisuUtil caiPiaoUtil;
    
    private JisuUtil(String propPath) {
		super();
		prop=PropertyUtil.getProp(propPath);
		APPKEY=prop.getProperty("APPKEY");
	}
    
    private JisuUtil(JisuConfig jisuConfig) {
		super();
		APPKEY=jisuConfig.getAPPKEY();
	}


	public static JisuUtil init(String propPath) {
		if (caiPiaoUtil==null) {
			caiPiaoUtil=new JisuUtil(propPath);
		}		
		return caiPiaoUtil;
	}
	public static JisuUtil init(JisuConfig jisuConfig) {
		if (caiPiaoUtil==null) {
			caiPiaoUtil=new JisuUtil(jisuConfig);
		}		
		return caiPiaoUtil;
	}
	

	/* 
	    * @Description:获取彩票开奖信息
	    * 
	    * @return:
	    * 
	    * caipiaoid	int		是	彩票ID
		* issueno	string	否	期号 不选默认是当前期
	    *
	    */
    @SuppressWarnings("unchecked")
	public  Map<String, Object> getCaiPiaoInfo(int caipiaoid,String issueno) { 
        try {
            String url = query + "?appkey=" + APPKEY + "&caipiaoid=" + caipiaoid + "&issueno=" + issueno;
        	Map<String, Object> response=(Map<String, Object>) HttpClientUtil.httpJson.getClient(url, Charset.UTF8, Map.class);
        	String status=(String) response.get("status");
            if (!status.equals("0")) {
            	log.error(response.get("msg")+"");
            	return null;
            } else {
                return (Map<String, Object>) response.get("result");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /* 
	    * @Description:获取彩票列表
	    * 
	    * @return:
	    *
	    */
    @SuppressWarnings("unchecked")
	public  List<Map<String, Object>> getCaiPiaoList() { 
	     try {
	    	String url = type + "?appkey=" + APPKEY;
	     	Map<String, Object> response=(Map<String, Object>) HttpClientUtil.httpJson.getClient(url, Charset.UTF8, Map.class);
        	String status=(String) response.get("status");
            if (!status.equals("0")) {
	         	log.error(response.get("msg")+"");
	         	return null;
	        } else {
	             return (List<Map<String, Object>>) response.get("result");
	        }
	     } catch (Exception e) {
	         e.printStackTrace();
	         return null;
	     }
    }
    /* 
	    * @Description:获取彩票开奖历史信息
	    * 
	    * @return:
	    * caipiaoid	int		是	彩票ID
		* issueno	string	否	期号 不选默认是当前期
		* num		int		否	获取数量 最大20 默认10
		* start		int		否	起始位置 默认0
	    *
	    */
    @SuppressWarnings("unchecked")
	public  Map<String, Object> getHistory(int caipiaoid,String issueno) { 
	     try {
	        String url = history + "?appkey=" + APPKEY + "&caipiaoid=" + caipiaoid + "&issueno=" + issueno;
	     	Map<String, Object> response=(Map<String, Object>) HttpClientUtil.httpJson.getClient(url, Charset.UTF8, Map.class);
        	String status=(String) response.get("status");
            if (!status.equals("0")) {
	         	log.error(response.get("msg")+"");
	         	return null;
	         } else {
	             return (Map<String, Object>) response.get("result");
	         }
	     } catch (Exception e) {
	         e.printStackTrace();
	         return null;
	     }
    }
    
    /* 
	    * @Description:查询彩票是否中奖
	    * 
	    * @return:
	    *
	    * caipiaoid		string	是	彩票ID
		* issueno		string	否	期号 默认为最新一期
		* number		string	是	彩票号码 红球
		* refernumber 	string	否	彩票剩余号码 蓝球
		* type			string	否	投注类型 1直选 2组三 3组六
	    */
    @SuppressWarnings({ "unchecked", "deprecation" })
	public  Map<String, Object> isWinning(int caipiaoid,String issueno,String number,String refernumber,String type) { 
	     try {
	    	String url = winning + "?appkey=" + APPKEY + "&caipiaoid=" + caipiaoid + "&issueno=" + issueno+"&number="+URLEncoder.encode(number).replace("+", "%20")+"&refernumber="+refernumber+"&type="+type;
		    Map<String, Object> response=(Map<String, Object>) HttpClientUtil.httpJson.getClient(url,  Charset.UTF8, Map.class);
        	String status=(String) response.get("status");
            if (!status.equals("0")) {
	         	log.error(response.get("msg")+"");
	         	return null;
	         } else {
	             return (Map<String, Object>) response.get("result");
	         }
	     } catch (Exception e) {
	         e.printStackTrace();
	         return null;
	     }
    }
    
    /* 
	    * @Description:发送短信
	    * 
	    * @return:
	    * count:发送成功条数
	    * accountid:子账号ID
	    */
	@SuppressWarnings("unchecked")
	public  Map<String, Object> sendMsg(String phone,String content) { 
	     try {
	    	String url = msg +"?mobile="+phone+"&content="+content+ "&appkey=" + APPKEY ;
	    	System.err.println(url);
		    Map<String, Object> response=(Map<String, Object>) HttpClientUtil.httpJson.getClient(url,  Charset.UTF8, Map.class);
		    String status=(String) response.get("status");
		    if (!status.equals("0")) {
	         	log.error(response.get("msg")+"");
	         	return null;
	         } else {
	             return (Map<String, Object>) response.get("result");
	         }
	     } catch (Exception e) {
	         e.printStackTrace();
	         return null;
	     }
 }
}
